import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:laos_mall/common/config/app_const.dart';
import 'package:laos_mall/common/style/app_style.dart';
import 'package:laos_mall/common/style/extension_util.dart';
import 'package:laos_mall/common/utils/num_util.dart';
import 'package:laos_mall/common/utils/widget_util.dart';
import 'package:laos_mall/common/widght/image_widget.dart';
import 'package:laos_mall/common/widght/item_widget.dart';
import 'package:laos_mall/common/widght/list_title_widget.dart';
import 'package:laos_mall/pages/accout/login/logic.dart';
import 'package:laos_mall/pages/accout/login/view.dart';
import 'package:laos_mall/pages/main/config.dart';
import 'package:laos_mall/user_data.dart';

import 'logic.dart';

class MyPage extends StatelessWidget {
  MyPage({Key? key}) : super(key: key);
  final logic = Get.put(MyLogic());

  @override
  Widget build(BuildContext context) {
    return Obx(() {
      if (UserData().isLogin.value == false) {
        Get.lazyPut(() => LoginLogic());
        return LoginPage(showClose: false,);
      }
      return GetBuilder<MyLogic>(builder: (logic) {
        return Scaffold(
        appBar: AppBar(
          actions: [
            Image.asset('ic_setting'.toImgPath(), width: 20.w, height: 20.w)
                .addPadding(EdgeInsets.only(right: 20.w))
                .addGestureTap(logic.jumpSetting)
          ],
        ),
        body: _buildLogin(),
        );
      });
    });
  }

  _buildLogin() {
    return Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
      ListTitleWidget(
        paddingOfLeft: EdgeInsets.only(right: 16.w),
        spaceRight: 0,
        leading: ImageWidget(
          url: logic.userModel?.avatar,
          width: 40.h,
          height: 40.h,
          fit: BoxFit.fill,
        ).addCircleAvatar(),
        mainAxisAlignment: MainAxisAlignment.center,
        title: Row(
          children: [
            Text("${logic.userModel?.nickname}"),
            // ItemWidget(
            //   prefixItem: Image.asset(
            //     'ic_zuansi'.toImgPath(),
            //     width: 14.h,
            //     height: 12.h,
            //   ),
            //   suffixItem: Text("DIAMOND MEMBER"),
            // )
          ],
        ),
        subtitle: Text(
          "TEL:${logic.userModel?.mobile}",
          style: AppStyle.textSmallStyleLightGray,
        ),
      ).addContainer(height: 60.h).addGestureTap(logic.jumpUserInfo),
      WidgetUtil.getHeaderInfo(
          'member_qr_code'.tr,
          AppButton.sizeBtn(
              logic.showQRCode,
              Row(
                mainAxisSize: MainAxisSize.min,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Image.asset(
                    'qrcode'.toImgPath(),
                    width: 20.w,
                    height: 20.w,
                    fit: BoxFit.fill,
                  ),
                  SizedBox(
                    width: 8.w,
                  ),
                  Text(
                    "show_qr_code".tr,
                    style: AppStyle.subtitleLightStyleGray,
                  ),
                ],
              ),
              style: AppButtonStyle.hollowBlack,
              size: Size(160.w, 34.h))).addPadding(EdgeInsets.only(top: 12.h)),
      _buildOrder(),
      _buildWallet(),
      _buildService()
    ]
    )
        .addPadding(EdgeInsets.symmetric(horizontal: 20.w))
        .addSingleChildScrollView();
  }

  _buildOrder() {
    return WidgetUtil.getHeaderInfo(space: 0, "my_order".tr, ListView.builder(
      itemBuilder: (context, index) {
        return ListTitleWidget(
          paddingOfLeft: EdgeInsets.zero,
          spaceRight: 0,
          mainAxisAlignment: MainAxisAlignment.center,
          title: Text(logic.orderTitles[index].tr,
            style: AppStyle.subtitleLightStyleGray,),
        ).addContainer(height: 46.h).addGestureTap(() {
          logic.jumpOrder(index);
        }).addDiver();
      },
      shrinkWrap: true,
      itemCount: 3,
      padding: EdgeInsets.zero,
      physics: const NeverScrollableScrollPhysics(),)).addPadding(
        EdgeInsets.only(top: 30.h));
  }

  _buildWallet() {
    return WidgetUtil.getHeaderInfo(space: 0, "my_wallet".tr, Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisSize: MainAxisSize.min,
      children: [
        SizedBox(height: 10.h,),
        AppButton.sizeBtn(
          logic.jumpBalance,
          Row(
            mainAxisSize: MainAxisSize.min,
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              Image.asset(
                'ic_wallet_line'.toImgPath(),
                width: 16.w,
                height: 16.w,
                fit: BoxFit.fill,
              ),
              SizedBox(
                width: 7.w,
              ),
              Text(
                "balance".tr,
                style: AppStyle.subtitleLightStyleGray,
              ),
              SizedBox(
                width: 5.w,
              ),
              Text('${Config.symbol}${NumUtils.format(logic.userModel?.balance)}', style: AppStyle.subtitleStyle,
                textAlign: TextAlign.right,).addExpand()
            ],
          ),
          style: AppButtonStyle.hollowBlack,
          size: Size(210.w, 38.h),).addPadding(EdgeInsets.only(top: 10.h)),
        AppButton.sizeBtn(
            logic.jumpCoupon,
            Row(
              mainAxisSize: MainAxisSize.min,
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                Image.asset(
                  'ic_cupon'.toImgPath(),
                  width: 16.w,
                  height: 16.w,
                  fit: BoxFit.fill,
                ),
                SizedBox(
                  width: 7.w,
                ),
                Text(
                  "coupons".tr,
                  style: AppStyle.subtitleLightStyleGray,
                ),
                SizedBox(
                  width: 5.w,
                ),
                Text('${logic.userModel?.coupon}', style: AppStyle.subtitleStyle,
                  textAlign: TextAlign.right,).addExpand()
              ],
            ),
            style: AppButtonStyle.hollowBlack,
            size: Size(210.w, 38.h)).addPadding(EdgeInsets.only(top: 10.h)),
        AppButton.sizeBtn(
            logic.jumpPoint,
            Row(
              mainAxisSize: MainAxisSize.min,
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                Image.asset(
                  'ic_sanbin'.toImgPath(),
                  width: 16.w,
                  height: 16.w,
                  fit: BoxFit.fill,
                ),
                SizedBox(
                  width: 7.w,
                ),
                Text(
                  "points".tr,
                  style: AppStyle.subtitleLightStyleGray,
                ),
                SizedBox(
                  width: 5.w,
                ),
                Text('${Config.symbol}${NumUtils.format(logic.userModel?.points)}', style: AppStyle.subtitleStyle,
                  textAlign: TextAlign.right,).addExpand()
              ],
            ),
            style: AppButtonStyle.hollowBlack,
            size: Size(210.w, 38.h)).addPadding(EdgeInsets.only(top: 10.h)),
      ],).addContainer(width: 210.w)).addPadding(EdgeInsets.only(top: 30.h));
  }

  _buildService() {
    return WidgetUtil.getHeaderInfo(space: 0, "my_order".tr, ListView.builder(
        physics: const NeverScrollableScrollPhysics(),
        shrinkWrap: true,
        padding: EdgeInsets.zero,
        itemCount: logic.walletTitles.length,
        itemBuilder: (_, index) {
          return ListTitleWidget(
            paddingOfLeft: EdgeInsets.zero,
            spaceRight: 0,
            mainAxisAlignment: MainAxisAlignment.center,
            title: Text(logic.walletTitles[index].tr,
              style: AppStyle.subtitleLightStyleGray,),
          ).addContainer(height: 46.h).addDiver().addGestureTap(() {
            logic.clickOrder(index);
          });
        })).addPadding(EdgeInsets.only(top: 30.h, bottom: 30.h));
  }
}
